package com.team9.fitness.mapper;

import com.team9.fitness.entity.ExerciseRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 运动记录数据访问层接口
 * 
 * 提供运动记录数据的CRUD操作：
 * - 运动记录管理：插入、更新、删除运动记录
 * - 查询功能：支持按用户、状态、日期、运动类型查询
 * - 数据关联：与运动信息表关联，获取运动名称和特性
 * - 状态管理：支持进行中、已完成等状态筛选
 * 
 * 数据库操作：
 * - 使用MyBatis注解方式定义SQL
 * - 支持LEFT JOIN关联查询
 * - 提供运动类型特性查询（是否有速度、心率）
 */
@Mapper
public interface ExerciseRecordMapper {

        @Insert("INSERT INTO exercise_record (user_id, exercise_id, start_time, end_time, duration_seconds, " +
                        "avg_heart_rate, max_heart_rate, avg_speed, max_speed, status, created_at) " +
                        "VALUES (#{userId}, #{exerciseId}, #{startTime}, #{endTime}, #{duration.toSeconds()}, " +
                        "#{avgHeartRate}, #{maxHeartRate}, #{avgSpeed}, #{maxSpeed}, #{status}, NOW())")
        @Options(useGeneratedKeys = true, keyProperty = "id")
        int insert(ExerciseRecord exerciseRecord);

        @Select("SELECT er.*, ei.name as exerciseName, ei.has_speed as hasSpeed, ei.has_heart_rate as hasHeartRate FROM exercise_record er "
                        +
                        "LEFT JOIN exercise_info ei ON er.exercise_id = ei.id " +
                        "WHERE er.id = #{id}")
        ExerciseRecord selectById(Long id);

        @Select("SELECT er.*, ei.name as exerciseName, ei.has_speed as hasSpeed, ei.has_heart_rate as hasHeartRate FROM exercise_record er "
                        +
                        "LEFT JOIN exercise_info ei ON er.exercise_id = ei.id " +
                        "WHERE er.user_id = #{userId} ORDER BY er.start_time DESC")
        List<ExerciseRecord> selectByUserId(Long userId);

        @Select("SELECT er.*, ei.name as exerciseName, ei.has_speed as hasSpeed, ei.has_heart_rate as hasHeartRate FROM exercise_record er "
                        +
                        "LEFT JOIN exercise_info ei ON er.exercise_id = ei.id " +
                        "WHERE er.user_id = #{userId} AND er.status = #{status} ORDER BY er.start_time DESC")
        List<ExerciseRecord> selectByUserIdAndStatus(Long userId, String status);

        @Select("SELECT er.*, ei.name as exerciseName, ei.has_speed as hasSpeed, ei.has_heart_rate as hasHeartRate FROM exercise_record er "
                        +
                        "LEFT JOIN exercise_info ei ON er.exercise_id = ei.id " +
                        "WHERE er.user_id = #{userId} AND DATE(er.start_time) = DATE(#{date}) ORDER BY er.start_time DESC")
        List<ExerciseRecord> selectByUserIdAndDate(Long userId, LocalDateTime date);

        @Select("SELECT er.*, ei.name as exerciseName, ei.has_speed as hasSpeed, ei.has_heart_rate as hasHeartRate FROM exercise_record er "
                        +
                        "LEFT JOIN exercise_info ei ON er.exercise_id = ei.id " +
                        "WHERE er.user_id = #{userId} AND er.exercise_id = #{exerciseId} ORDER BY er.start_time DESC")
        List<ExerciseRecord> selectByUserIdAndExerciseId(Long userId, Long exerciseId);

        @Update("UPDATE exercise_record SET end_time = #{endTime}, duration_seconds = #{durationSeconds}, " +
                        "avg_heart_rate = #{avgHeartRate}, max_heart_rate = #{maxHeartRate}, " +
                        "avg_speed = #{avgSpeed}, max_speed = #{maxSpeed}, status = #{status} WHERE id = #{id}")
        int update(ExerciseRecord exerciseRecord);

        @Delete("DELETE FROM exercise_record WHERE id = #{id}")
        int deleteById(Long id);

        @Delete("DELETE FROM exercise_record WHERE user_id = #{userId}")
        int deleteByUserId(Long userId);
}
